package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.sggg.entity.SgCategory;
import org.jeecg.modules.sggg.entity.SgGoodsRotation;
import org.jeecg.modules.sggg.entity.SgMember;
import org.jeecg.modules.sggg.service.ISgGoodsRotationService;
import org.jeecg.modules.sggg.service.ISgMemberService;
import org.jeecg.modules.system.controller.SysRoleController;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 商品轮播信息管理
 * @Author: jeecg-boot
 * @Date: 2023-04-13
 * @Version: V1.0
 */
@Api(tags = "商品轮播信息管理")
@RestController
@RequestMapping("/goodsRotation/sgGoodsRotation")
@Slf4j
public class SgGoodsRotationController extends JeecgController<SgGoodsRotation, ISgGoodsRotationService> {

    @Autowired
    private ISgGoodsRotationService sgGoodsRotationService;

    @Autowired
    private SysRoleController sysRoleController;

    @Autowired
    private ISgMemberService sgMemberService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ISysDepartService sysDepartController;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页列表查询
     *
     * @param sgGoodsRotation
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "商品轮播信息管理-分页列表查询")
    @ApiOperation(value = "商品轮播信息管理-分页列表查询", notes = "商品轮播信息管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgGoodsRotation>> queryPageList(SgGoodsRotation sgGoodsRotation,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<SgGoodsRotation> queryWrapper = QueryGenerator.initQueryWrapper(sgGoodsRotation, req.getParameterMap()).eq("is_delete", 0);
        String roleCode = redisUtil.get("roleCode:" + sysUser.getId()).toString();
        SysDepart sysDepart = sysDepartController.getById(sysUser.getDepartIds());
        // 根据DeptAttr机构属性判断当前登录人属于哪个机构（三高之家、三高基地、三高中心）
        if(sysDepart !=null && sysDepart.getDeptAttr()!=null){
            SysUser sysUser1 = sysUserService.getById(sysUser.getId());
            // 1.三高之家
            if(sysDepart.getDeptAttr().equals("1")){
                queryWrapper.eq("sys_org_code", sysUser.getOrgCode().toString());
            }
            // 2.三高基地
            if(sysDepart.getDeptAttr().equals("2")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                queryWrapper.in("sys_org_code", responsibleAgencies);
            }
            // 3.三高中心
            if(sysDepart.getDeptAttr().equals("3")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                queryWrapper.in("sys_org_code", responsibleAgencies);
            }
        }else if(roleCode.contains("dept_manage") || roleCode.contains("admin")){
            queryWrapper.like("sys_org_code",sysUser.getOrgCode());

        }else{
            queryWrapper.eq("sys_org_code", sysUser.getOrgCode().toString());
        }
        Page<SgGoodsRotation> page = new Page<SgGoodsRotation>(pageNo, pageSize);
        IPage<SgGoodsRotation> pageList = sgGoodsRotationService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param sgGoodsRotation
     * @return
     */
    @AutoLog(value = "商品轮播信息管理-添加")
    @ApiOperation(value = "商品轮播信息管理-添加", notes = "商品轮播信息管理-添加")
    //@RequiresPermissions("goodsRotation:sg_goods_rotation:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgGoodsRotation sgGoodsRotation) {
        sgGoodsRotationService.save(sgGoodsRotation);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sgGoodsRotation
     * @return
     */
    @AutoLog(value = "商品轮播信息管理-编辑")
    @ApiOperation(value = "商品轮播信息管理-编辑", notes = "商品轮播信息管理-编辑")
    //@RequiresPermissions("goodsRotation:sg_goods_rotation:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgGoodsRotation sgGoodsRotation) {
        sgGoodsRotationService.updateById(sgGoodsRotation);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品轮播信息管理-通过id删除")
    @ApiOperation(value = "商品轮播信息管理-通过id删除", notes = "商品轮播信息管理-通过id删除")
    //@RequiresPermissions("goodsRotation:sg_goods_rotation:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        sgGoodsRotationService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商品轮播信息管理-批量删除")
    @ApiOperation(value = "商品轮播信息管理-批量删除", notes = "商品轮播信息管理-批量删除")
    //@RequiresPermissions("goodsRotation:sg_goods_rotation:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sgGoodsRotationService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "商品轮播信息管理-通过id查询")
    @ApiOperation(value = "商品轮播信息管理-通过id查询", notes = "商品轮播信息管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgGoodsRotation> queryById(@RequestParam(name = "id", required = true) String id) {
        SgGoodsRotation sgGoodsRotation = sgGoodsRotationService.getById(id);
        if (sgGoodsRotation == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgGoodsRotation);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sgGoodsRotation
     */
    //@RequiresPermissions("goodsRotation:sg_goods_rotation:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgGoodsRotation sgGoodsRotation) {
        return super.exportXls(request, sgGoodsRotation, SgGoodsRotation.class, "商品轮播信息管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("goodsRotation:sg_goods_rotation:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgGoodsRotation.class);
    }


    /**
     * TODO 列表查询
     *
     * @param
     * @return
     * @author WangJie
     * @date 2023/4/13 14:54
     * @Exception
     */
    @ApiOperation(value = "商品轮播信息管理-列表查询", notes = "商品轮播信息管理-列表查询")
    @GetMapping(value = "/queryList")
    public Result<List<SgGoodsRotation>> queryList(String memberId) {
        SgMember member = sgMemberService.getById(memberId);
        List<SgGoodsRotation> List = sgGoodsRotationService.list(new QueryWrapper<SgGoodsRotation>().eq("is_show", 0).eq("is_delete", 0).eq("sys_org_code", member.getSysOrgCode()).orderByAsc("order_number"));
        return Result.OK(List);
    }

    /**
     * TODO     轮播上下架
     *
     * @param
     * @return
     * @author WangJie
     * @date 2023/3/31 9:10
     * @Exception
     */
    @AutoLog(value = "商品类型表-轮播上下架")
    @ApiOperation(value = "商品类型表-轮播上下架", notes = "商品类型表-轮播上下架")
    @RequestMapping(value = "/isShow", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> isOnSale(@RequestBody SgGoodsRotation sgGoodsRotation) {
        sgGoodsRotation.setIsShow(sgGoodsRotation.getIsShow() == 0 ? 1 : 0);
        sgGoodsRotationService.updateById(sgGoodsRotation);
        return Result.OK(sgGoodsRotation.getIsShow() == 0 ? "取消展示成功!" : "展示成功!");
    }
}
